package com.stylefeng.guns.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 获取查询时间的最后一天
     * @param date
     * @return
     */

    public static Date getSearchEndDate(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date endDate = null;
        try {
            //对比当前时间和查询时间是否为通一月
            Calendar c1 = Calendar.getInstance();
            c1.setTime(date);
            c1.set(Calendar.MONTH,date.getMonth()+1);
            c1.set(Calendar.DAY_OF_MONTH,1);
            endDate = dateFormat.parse(simpleDateFormat.format(c1.getTime()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return endDate;
    }


    public static Date getSearchBeginDate(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = null;
        try {
            Calendar c1 = Calendar.getInstance();
            c1.setTime(date);
            c1.set(Calendar.DAY_OF_MONTH,1);
            c1.set(Calendar.MONTH,date.getMonth());
            beginDate = dateFormat.parse(simpleDateFormat.format(c1.getTime()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return beginDate;
    }

    public static Date getSearchYearDate(Date date){
        int year = date.getYear();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = null;
        try {
            Calendar c1 = Calendar.getInstance();
            c1.clear();
            c1.set(Calendar.YEAR,year);
            beginDate = dateFormat.parse(simpleDateFormat.format(c1.getTime()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return beginDate;
    }

    public static void main(String[] args)throws Exception {
        DateUtils dateUtils = new DateUtils();
        String date = "2018-12-06";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(dateUtils.getSearchEndDate(simpleDateFormat.parse(date)));
    }


}
